<?php

namespace app\common\func;


use app\common\model\EquipBuyNums;
use app\common\model\OrderCart;
use app\common\model\OrderProduct;
use app\common\model\PolicyStandard;
use app\common\model\Rate;
use app\common\model\StandardRecord;
use app\common\model\UserAddress;
use fast\Random;
use think\Db;

class Order
{

    //stats：0 = 所有订单，1 = 已支付，2 = 已完成，
    public static function getUserOrderCount(array $userId,$state = 0){
        $where = ["user_id" => ['in',$userId]];
        if($state == 1){
            $where['pay_state'] = 1;
        }elseif ($state == 2){
            $where['pay_state'] = 2;
        }else{
            $where['pay_state'] = ['in',[1,2]];
        }
        return \app\common\model\Order::where($where)->count();
    }

    public static function getBuyNumber($userId){
        return EquipBuyNums::where(['agid' => $userId])->find();
    }

    public static function getOrderTotalNumber($orderId){
        return OrderProduct::where(['order_id' => $orderId])->sum("number");
    }

    public static function getOrderProductFinishCount($orderId){
        return OrderProduct::where(['order_id' => $orderId,'status' => 2])->count();
    }

    public static function getOrderProductCount($orderId){
        return OrderProduct::where(['order_id' => $orderId])->count();
    }

    public static function getOrderProductById($orderProductId){
        return OrderProduct::get($orderProductId);
    }

    public static function getOrderProduct($orderId,$productId){
        return OrderProduct::where(['order_id' => $orderId,'product_id' => $productId])->find();
    }

    public static function getRecordBySn($sn){
        return StandardRecord::where(['sn' => $sn,"status" => 0])->select();
    }

    public static function getRateByIds($rateIds){
        return Rate::where(["id" => ["in",$rateIds]])->select();
    }

    public static function getStandardByPolicy($policyId){
        return PolicyStandard::where(["policy_id" => $policyId])->order("day_limit asc")->select();
    }

    public static function getCartByProduct($userId,$productId){
        return OrderCart::where(['user_id' => $userId,"product_id" => $productId])->find();
    }

    public static function getCartById($userId,$cartId){
        return OrderCart::where(['user_id' => $userId,"id" => $cartId])->find();
    }

    public static function getCartByUser($userId){
        return Db::name("order_cart")->alias("c")
            ->join("product p","c.product_id = p.id","LEFT")
            ->where(['c.user_id' => $userId])
            ->field("c.*,p.name,p.price,p.image,p.min,0 as checked")
            ->select();
    }

    public static function getUserBalance($userId){
        return \app\common\model\Balance::where(["main_type" => 2,"main_id" => $userId])->find();
    }

    public static function getOrderByOrderNum($orderNum){
        return \app\common\model\Order::where(["order_num" => $orderNum])->find();
    }

    public static function getOrderById($orderId){
        return \app\common\model\Order::get($orderId);
    }

    public static function getUserAddressById($userId,$addressId){
        return UserAddress::where(["user_id" => $userId,"id" => $addressId])->find();
    }

    public static function getOrderNum(){
        return "MAX".date("YmdHis").Random::numeric(6);
    }

    public static function getOrderRefundNum(){
        return "Refund".date("YmdHis").Random::numeric(6);
    }

    public static function getUserAddress($userId){
        return UserAddress::where(["user_id" => $userId])->order("default desc")->select();
    }

    public static function getUserDefaultAddress($userId){
        return UserAddress::where(["user_id" => $userId,"default" => 1])->select();
    }
}
